package com.terrace.web.tagext.easyui.support;

import org.apache.commons.lang.StringUtils;

import com.terrace.core.utils.JsonUtils;
import com.terrace.web.tagext.easyui.utils.EasyUIUtils;

/**
 * 下拉框自定义扩展标签
 * 
 * @author jiangyg
 *
 */
public class ComboTagSupport extends ValidateboxTagSupport {

	private static final long serialVersionUID = 6450492156435251527L;

	/**
	 * combo名
	 */
	protected String name;

	/**
	 * 组合宽度
	 */
	protected Integer width;

	/**
	 * 组合高度
	 */
	protected Integer height;

	/**
	 * 组合下拉面板宽度
	 */
	protected Integer panelWidth;

	/**
	 * 组合下拉面板高度
	 */
	protected Integer panelHeight;
	
	/**
	 * 默认单个选项--单选时用到
	 */
	protected String setValue;
	
	/**
	 * 默认多个选项--多选时用到
	 */
	protected String[] setValues;
	
	/**
	 * 是否可以编辑--默认不可编辑
	 */
	protected boolean editable = false;

	/**
	 * 是否禁用
	 */
	protected boolean disabled = false;

	/**
	 * 是否只读
	 */
	protected boolean readonly = false;
	
	/**
	 * 是否多选
	 */
	protected boolean multiple = false;
	
	@Override
	public String assembleDataOptions() {
		return assembleBaseDataOptions() + "," + assembleExtendDataOptions();
	}
	
	@Override
	public String assembleExtendDataOptions() {
		StringBuffer sb = new StringBuffer();
		EasyUIUtils.appendDatsOption(sb, "width", width);
		EasyUIUtils.appendDatsOption(sb, "height", height);
		EasyUIUtils.appendDatsOption(sb, "disabled", disabled);
		EasyUIUtils.appendDatsOption(sb, "readonly", readonly);
		EasyUIUtils.appendDatsOption(sb, "multiple", multiple);
		EasyUIUtils.appendDatsOption(sb, "editable", editable);
		EasyUIUtils.appendDatsOption(sb, "panelWidth", panelWidth);
		EasyUIUtils.appendDatsOption(sb, "panelHeight", panelHeight);
		return EasyUIUtils.substringLastComma(sb).toString();
	}
	
	/**
	 * 设置组合框默认值
	 * @return
	 */
	public String setComboValue() {
		StringBuffer sb = new StringBuffer();
		// 单选有默认值
		if (!multiple && StringUtils.isNotBlank(setValue)) {
			sb.append("<script type=\"text/javascript\">$(function() {$('#" + name + "').combobox('setValue', '" + setValue + "');});</script>");
		}
		
		// 多选有默认值
		if (multiple && setValues != null && setValues.length > 0) {
			sb.append("<script type=\"text/javascript\">$(function() {$('#" + name + "').combobox('setValues', '" + JsonUtils.beanToJson(setValues) + "');});</script>");
		}
		return sb.toString();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getWidth() {
		return width;
	}

	public void setWidth(Integer width) {
		this.width = width;
	}

	public Integer getHeight() {
		return height;
	}

	public void setHeight(Integer height) {
		this.height = height;
	}

	public Integer getPanelWidth() {
		return panelWidth;
	}

	public void setPanelWidth(Integer panelWidth) {
		this.panelWidth = panelWidth;
	}

	public Integer getPanelHeight() {
		return panelHeight;
	}

	public void setPanelHeight(Integer panelHeight) {
		this.panelHeight = panelHeight;
	}

	public String getSetValue() {
		return setValue;
	}

	public void setSetValue(String setValue) {
		this.setValue = setValue;
	}

	public String[] getSetValues() {
		return setValues;
	}

	public void setSetValues(String[] setValues) {
		this.setValues = setValues;
	}

	public boolean isEditable() {
		return editable;
	}

	public void setEditable(boolean editable) {
		this.editable = editable;
	}

	public boolean isDisabled() {
		return disabled;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	public boolean isReadonly() {
		return readonly;
	}

	public void setReadonly(boolean readonly) {
		this.readonly = readonly;
	}

	public boolean isMultiple() {
		return multiple;
	}

	public void setMultiple(boolean multiple) {
		this.multiple = multiple;
	}

}
